import json

class TaskParser:
    """
    任务解析类
    """
    def __init__(self):
        self.warehouse_Status = []
        self.json = self.read_json()


    def read_json(self, FILE_NAME='MissionList.json'):
        """
        读取json数据
        """
        with open(FILE_NAME) as f:
            temp = json.loads(f.read())
        return temp

    # 修改目标点位移动后的状态
    def switch_position_status(self, warehouseStatus=[], wh_col=0, wh_posi=0, new_point=0):
        whStatus_col = warehouseStatus[wh_col]
        old_point = whStatus_col[wh_posi]
        whStatus_col[wh_posi] = new_point
        return whStatus_col[wh_posi], old_point

    # 读取设备点
    def get_plc_point(self, json_data, StackerPosition:str = "StackerPosition"):
        return json_data[StackerPosition]

    # 读取任务点
    def get_task_target_point(self, task):
        description = task['Description']
        for desc in description:
            target = desc['target']
        return target

    # 读取任务类型
    def get_task_type(self, task):
        description = task['Description']
        for desc in description:
            task_type = desc['Type']
        return task_type

    # 读取任务库排号
    def get_task_warehouseStatus(self, task):
        description = task['Description']
        for desc in description:
            warehouseStatus = desc['warehouseStatus']
        return warehouseStatus

    # 从总任务表读取出库口位置
    def get_out_point(self, json_data, tasks_list:str = "Tasks", task_number = 0):
        task = json_data[tasks_list][task_number]
        description = task['Description']
        for desc in description:
            out_position = desc['out_pos']
        return out_position
    # 从Task子任务中读取出库口位置
    def get_task_outpoint(self, task):
        description = task['Description']
        for desc in description:
            out_position = desc['out_pos']
        return out_position

    # 从总任务表读取入库口位置
    def get_in_point(self, json_data, tasks_list:str = "Tasks", task_number = 0):
        task = json_data[tasks_list][task_number]
        description = task['Description']
        for desc in description:
            in_position = desc['in_pos']
        return in_position
    # 从Task子任务中读取入库口位置
    def get_task_inpoint(self, task):
        description = task['Description']
        for desc in description:
            out_position = desc['in_pos']
        return out_position

    # 判断是否有遮挡
    def is_hinder(self, task):
        description = task['Description']
        for desc in description:
            wh_Status = desc['warehouseStatus']
        return wh_Status


######################################
################ 测试代码 #############
######################################

########### 读取json数据 ###########
# json_data = read_json()
# print(json_data)

########### 读取是否遮挡 ###########


########### 读取任务点 ###########
# tasks_list = json_data["Tasks"]
# print(tasks_list)
# task_num = len(json_data["Tasks"])
# print(f'子任务数量: {task_num}')
# for i in range(task_num):
#     task = tasks_list[i]
#     ttp = get_task_target_point(task)
#     print(f'子任务点：{ttp}')

########### 读取任务库排号 ###########
# tasks_list = json_data["Tasks"]
# task_num = len(json_data["Tasks"])
# for i in range(task_num):
#     task = tasks_list[i]
#     tw = get_task_warehouseStatus(task)
#     ttp = get_task_target_point(task)
#     print(f'子任务点：{ttp}, 子任务库排号：{tw}')

########### 读取出库口 ###########
# out_position = get_out_point(json_data)
# print(f'出库口位置：{out_position}')

########### 读取入库口 ###########
# in_position = get_in_point(json_data)
# print(f'入库口位置：{in_position}')

########### 读取任务类型 ###########
# tasks_list = json_data["Tasks"]
# task_num = len(json_data["Tasks"])
# print(f'子任务数量: {task_num}')
# for i in range(task_num):
#     task = tasks_list[i]
#     tt = get_task_type(task)
#     print(f'子任务类型：{tt}')

########### 读取设备点 ###########
# sp = get_plc_point(json_data)
# print(f'读取设备点：{sp}')

########### 测试修改目标点位移动后的状态模块 ###########
# # 查看修改前，库排的所有库位状态
# StackerPosition = "StackerPosition"
# for i in range(len(json_data["warehouseStatus"])):
#     print(f'修改前的 - 库排{i}: {json_data["warehouseStatus"][i]}')
# print('==================================================')

# # 修改目标点位移动后的状态
# warehouseStatus = json_data["warehouseStatus"]
# wh_col=0
# wh_posi=0
# new_point=2
# new, old = switch_position_status(warehouseStatus, wh_col, wh_posi, new_point)

# # 查看修改后，库排的所有库位状态
# for i in range(len(json_data["warehouseStatus"])):
#     print(f'修改后的 - 库排{i}: {json_data["warehouseStatus"][i]}')
# print('==================================================')
# # 修改前和修改后的状态码
# print(f'old status:{old}, new status:{new}')